package com.haoze.nurseapp.controller;


import com.haoze.common.config.RedisUtil;
import com.haoze.common.enumeration.CirculationModelEnum;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurseapp.dto.Assessment.CommonTaskCirculationDTO;
import com.haoze.nurseapp.dto.PatientInHospInfo;
import com.haoze.nurseapp.dto.PatientInHospitalDTO;
import com.haoze.nurseapp.dto.UserDept;
import com.haoze.nurseapp.service.NurseFileIndexService;
import com.haoze.nurseapp.service.PatientCirculationService;
import com.haoze.nurseapp.service.PatientService;
import com.haoze.nurseapp.service.feign.AssessmentScaleService;
import com.haoze.nurseapp.vo.PatientLeaveHospitalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.POST;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/nurseapp/circulation")
@Api(value = "病人流转-入院")
public class PatientCirculationController {

    @Autowired
    PatientCirculationService patientCirculationService;

    @Autowired
    NurseFileIndexService nurseFileIndexService;

    @Autowired
    PatientService patientService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    AssessmentScaleService assessmentScaleService;


    //@Autowired
    //private MongoTemplate doctorMongoTemplate;

    @GetMapping("/queryTampList")
    @ApiOperation(value = "根据模型名称获取对应模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelName", value = "模型名称", dataType = "String", paramType = "modelName"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryTampList(@RequestParam final String modelName, @RequestParam final String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.queryTampList(modelName, inpatNum,circulationlLogId);
    }

    @GetMapping("/queryOperationTampList")
    @ApiOperation(value = "专门获取查看手术转运交接")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelName", value = "模型名称", dataType = "String", paramType = "modelName"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryOperationTampList(@RequestParam final String modelName, @RequestParam final String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.queryOperationTampList(modelName, inpatNum,circulationlLogId);
    }

    @GetMapping("/queryTransferTampList")
    @ApiOperation(value = "转科转入获取转科转出数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelName", value = "模型名称", dataType = "String", paramType = "modelName"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryTransferTampList(@RequestParam final String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.queryTransferTampList(inpatNum,circulationlLogId);
    }

    @GetMapping("/queryFileDataByTplId")
    @ApiOperation(value = "根据模板ID创建对应文书内容返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tplId", value = "模板ID", dataType = "String", paramType = "tplId"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "wardDeptId", value = "病区id", dataType = "String", paramType = "deptId")
    })
    public Result queryFileDataByTplId(@RequestParam final String tplId, @RequestParam final String inpatNum,
                                       @RequestParam final String wardDeptId,
                                       @RequestParam(required = false) String refEmrFileId,
                                       @RequestParam(required = false) String dailyAssessmentMarker) {
        return nurseFileIndexService.createFileIndex(tplId, inpatNum, wardDeptId, null, null,refEmrFileId,dailyAssessmentMarker);
    }

    @GetMapping("/queryDeptByWardDeptId")
    @ApiOperation(value = "根据病区找对应科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "dept/wardDept", dataType = "String", paramType = "type"),
            @ApiImplicitParam(name = "contentId", value = "病区id", dataType = "String", paramType = "deptId")
    })
    public Result queryDeptByWardDeptId(@RequestParam(required = false) final String type, @RequestParam final String contentId) {
        return patientCirculationService.queryDeptByWardDeptId(type, contentId);
    }

    @GetMapping("/cancleInHosp")
    @ApiOperation(value = "取消入院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result cancleInHosp(@RequestParam final String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.cancleInHosp(inpatNum,circulationlLogId);
    }

    @PostMapping("/saveAssessmentFileData")
    @ApiOperation(value = "保存评估文书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commonTaskCirculationDTO", value = "文书内容", dataType = "CommonTaskCirculationDTO", paramType = "commonTaskCirculationDTO")
    })
    public Result saveAssessmentFileData(@RequestBody CommonTaskCirculationDTO commonTaskCirculationDTO) {
        return patientCirculationService.saveAssessmentFileData(commonTaskCirculationDTO);
    }

    @GetMapping("/queryBedByWardDeptId")
    @ApiOperation(value = "病区可用床号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区id", dataType = "String", paramType = "deptId")
    })
    public Result queryBedByWardDeptId(@RequestParam final String wardDeptId) {
        return patientCirculationService.queryBedByWardDeptId(wardDeptId);
    }

    @GetMapping("/queryNurseLeaderByWardDeptId")
    @ApiOperation(value = "病区和床号对应护士组长及其他组长接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区id", dataType = "String", paramType = "wardDeptId"),
            @ApiImplicitParam(name = "bedNo", value = "床号", dataType = "String", paramType = "bedNo")
    })
    public Result queryNurseLeaderByWardDeptId(@RequestParam final String wardDeptId, @RequestParam final String bedNo) {
        return patientCirculationService.queryNurseLeaderByWardDeptId(wardDeptId, bedNo);
    }

    @PostMapping("/savePatientInfo")
    @ApiOperation(value = "把病人从待入院状态改为已入院接口，保存病人基本信息，调用HIS接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", dataType = "String", paramType = "doctorId"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "bedNo", value = "床号", dataType = "String", paramType = "bedNo"),
            @ApiImplicitParam(name = "nurseId", value = "分组护士组长", dataType = "String", paramType = "nurseId"),
            @ApiImplicitParam(name = "wardDeptId", value = "病区Id", dataType = "String", paramType = "wardDeptId"),
            @ApiImplicitParam(name = "deptId", value = "科室Id", dataType = "String", paramType = "deptId")
    })
    public Result savePatientInfo(@RequestBody final Map<String, String> param) {
        //return patientCirculationService.savePatientInfo(param.get("inpatNum"), param.get("wardDeptId"));
        return patientCirculationService.savePatientInfo(param);
    }

    @PostMapping("/saveOperator")
    @ApiOperation(value = "记录每日评估，入院宣教是否完成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "circulationTemplateId", value = "模块配置Id", dataType = "String", paramType = "circulationTemplateId")
    })
    public Result saveOperator(@RequestBody final Map<String, String> param) {
        return patientCirculationService.saveOperator(param.get("inpatNum"), param.get("circulationTemplateId"), null,param.get("circulationlLogId"));
    }

    @GetMapping("/queryDepDoctor")
    @ApiOperation(value = "科室对应所有医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depId", value = "科室id", dataType = "String", paramType = "depId")
    })
    public Result queryDepDoctor(@RequestParam String depId) {
        return patientCirculationService.queryDepDoctorAndNursList(depId,null);
    }


    @GetMapping("/queryPatientDetailByInpatNum")
    @ApiOperation(value = "根据住院号查病人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryPatientDetailByInpatNum(@RequestParam String inpatNum) {
        return patientCirculationService.queryPatientCirculationDetailByInpatNum(inpatNum);
    }

    @GetMapping("/patientInfo")
    @ApiOperation(value = "病人信息-护理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "wardId", value = "类型", dataType = "String", paramType = "wardId"),
            @ApiImplicitParam(name = "keyWords", value = "关键字", dataType = "String", paramType = "keyWords")
    })
    public Result patientInfo(@RequestParam(required = false) String inpatNum,
                              @RequestParam(required = false) String wardId,
                              @RequestParam(required = false) String keyWords,
                              @RequestParam(required = false) String orgId,
                              @RequestParam(required = false) String circulationlLogId) {
        return ResultGenerator.genOkResult(patientService.getPatientInfoNurse(inpatNum, wardId, keyWords,orgId,circulationlLogId));
    }

    @PostMapping("/savePatientInfoInHosp")
    @ApiOperation(value = "保存病人信息-临时")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result savePatientInfoInHosp(@RequestBody final Map<String, Object> param) {
       return patientCirculationService.savePatientInfoInHosp(param);
    }

    @PostMapping("/savePatientInfoInDept")
    @ApiOperation(value = "保存病人信息-临时")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result savePatientInfoInDept(@RequestBody final Map<String, Object> param) {
        return patientCirculationService.savePatientInfoInDept(param);
    }

    @GetMapping("/getPatientInfoTemp")
    @ApiOperation(value = "查询病人信息-临时")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result getPatientInfoTemp(@RequestParam(required = false) String inpatNum,@RequestParam String circulationLogId) {
        //Map map = redisUtil.hmget(inpatNum+"_inHosp");
        //redisUtil.hmset(inpatNum+"_inHosp", param);
        /*Query query = new Query();
        query.addCriteria(Criteria.where("inpatNum").is(inpatNum));
        PatientInHospInfo patientInHospInfo = doctorMongoTemplate.findOne(query, PatientInHospInfo.class);*/
        /*if(null == patientInHospInfo){
            return ResultGenerator.genFailedResult("页面数据由于长时间未保存而失效");
        }*/
        return patientCirculationService.getPatientInfoTemp(inpatNum,circulationLogId);
    }

    @GetMapping("/getTransport")
    @ApiOperation(value = "病人转运方式")
    public Result getTransport() {
        return patientCirculationService.getTransport();
    }

    @PostMapping("/patientGoOut")
    @ApiOperation(value = "病人外出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientGoOut(@RequestBody final Map<String, String> param) {
        String inpatNum = param.get("inpatNum");
        String time = param.get("time");
        String reason = param.get("reason");

        return patientCirculationService.patientGoOut(inpatNum, time, reason);
    }

    @PostMapping("/patientRegression")
    @ApiOperation(value = "病人回归")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientRegression(@RequestBody final Map<String, String> param) {
        String inpatNum = param.get("inpatNum");
        String time = param.get("time");
        return patientCirculationService.patientRegression(inpatNum, time);
    }

    @GetMapping("/patientRotating")
    @ApiOperation(value = "病人转床")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientRotating(@RequestParam String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.patientRotating(inpatNum,circulationlLogId);
    }

    @GetMapping("/patientTransferOut")
    @ApiOperation(value = "病人转科转出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientTransferOut(@RequestParam String inpatNum,@RequestParam(required = false) String transport,
                                     @RequestParam String wardDeptId,@RequestParam String circulationlLogId,
                                     @RequestParam(required = false) String transferOutDateTime,
                                     @RequestParam(required = false) String stopDateTime,@RequestParam(required = false) String babyStopDateTime
                                     ) {
        return patientCirculationService.patientTransferOut(inpatNum,transport,wardDeptId,circulationlLogId,transferOutDateTime,stopDateTime,babyStopDateTime);
    }

    @GetMapping("/patientTransferOutPc")
    @ApiOperation(value = "pc端病人转科转出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientTransferOutPc(@RequestParam String inpatNum,@RequestParam(required = false) String circulationLogId,
                                     @RequestParam String transferOutWardDeptId,@RequestParam String transferOutDeptId,
                                     @RequestParam String transferInWardDeptId,@RequestParam String transferInDeptId,
                                     @RequestParam(required = false) String transferOutDateTime,@RequestParam(required = false) String stopDateTime,@RequestParam(required = false) String babyStopDateTime) {
        return patientCirculationService.patientTransferOutPc(inpatNum,circulationLogId,transferOutWardDeptId,transferOutDeptId,transferInWardDeptId,transferInDeptId,transferOutDateTime,stopDateTime,babyStopDateTime);
    }

    @GetMapping("/patientTransferIn")
    @ApiOperation(value = "病人转科转入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result patientTransferIn(@RequestParam String inpatNum,@RequestParam(required = false) String transport,
                                    @RequestParam(required = false) String bedNo,
                                    @RequestParam(required = false) String doctorPersonId,
                                    @RequestParam(required = false) String nurseId,
                                    @RequestParam String circulationlLogId,
                                    @RequestParam(required = false) String transferInDateTime) {
        return patientCirculationService.patientTransferIn(inpatNum,transport,bedNo,doctorPersonId,nurseId,circulationlLogId,transferInDateTime);
    }

    @GetMapping("/rotatingHistory")
    @ApiOperation(value = "转床历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "circulationlLogId", value = "流转日志Id", dataType = "String", paramType = "circulationlLogId")
    })
    public Result rotatingHistory(@RequestParam String inpatNum,@RequestParam String circulationlLogId) {
        return patientCirculationService.rotatingHistory(inpatNum,circulationlLogId);
    }

    /**
     * 
     * @Title: patientList   
     * @Description: TODO
     * @param: @param startDate
     * @param: @param endDate
     * @param: @param keyWords
     * @param: @param circulationType
     * @param: @param status
     * @param: @param inWardDeptId
     * @param: @param outWardDeptId
     * @param: @param wardDeptId
     * @param: @param type 1- 全部 6-手术
     * @param: @return      
     * @return: Result      
     * @throws
     */
    @GetMapping("/patientList")
    @ApiOperation(value = "流转病人列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "startDate"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "endDate"),
            @ApiImplicitParam(name = "keyWords", value = "关键字", dataType = "String", paramType = "keyWords"),
            @ApiImplicitParam(name = "circulationType", value = "流转类型", dataType = "String", paramType = "circulationType"),
            @ApiImplicitParam(name = "status", value = "待处理，已处理", dataType = "String", paramType = "status"),
            @ApiImplicitParam(name = "inWardDeptId", value = "转入病区代码", dataType = "String", paramType = "inWardDeptId"),
            @ApiImplicitParam(name = "outWardDeptId", value = "转出病区代码", dataType = "String", paramType = "outWardDeptId"),
            @ApiImplicitParam(name = "wardDeptId", value = "病区代码", dataType = "String", paramType = "wardDeptId")
    })
    public Result patientList(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate, @RequestParam(required = false) String keyWords,
                              @RequestParam(required = false) List<String> circulationType, @RequestParam(required = false) String status, @RequestParam(required = false) String inWardDeptId, @RequestParam(required = false) String outWardDeptId
            , @RequestParam(required = false) String wardDeptId) {
        return patientCirculationService.getPatientList(circulationType, status, endDate, startDate, keyWords, inWardDeptId, outWardDeptId, wardDeptId);
    }

    /**
     * 出院
     * @param patientLeaveHospitalVo
     * @return
     */
    @PostMapping("/patientLeaveHospital")
    @ApiOperation(value = "病人出院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "dischanargeModeName", value = "出院方式名称", dataType = "String", paramType = "dischanargeModeName"),
            @ApiImplicitParam(name = "dischanargeModeCode", value = "出院方式Code", dataType = "String", paramType = "dischanargeModeCode"),
            @ApiImplicitParam(name = "medicalCategory", value = "医疗类别", dataType = "String", paramType = "medicalCategory"),
            @ApiImplicitParam(name = "medicalCategoryCode", value = "医疗类别Code", dataType = "String", paramType = "medicalCategoryCode"),
            @ApiImplicitParam(name = "outDatetime", value = "出院时间", dataType = "String", paramType = "outDatetime"),
            @ApiImplicitParam(name = "diagName", value = "诊断名称", dataType = "String", paramType = "diagName"),
            @ApiImplicitParam(name = "circulationLogId", value = "日志id", dataType = "String", paramType = "circulationLogId"),
            @ApiImplicitParam(name = "stopDateTime", value = "停止时间", dataType = "String", paramType = "stopDateTime")
    })
    public Result patientLeaveHospital(@RequestBody PatientLeaveHospitalVo patientLeaveHospitalVo) {
        return patientCirculationService.patientLeaveHospital(patientLeaveHospitalVo.getInpatNum(),patientLeaveHospitalVo.getDischanargeModeName(),
                patientLeaveHospitalVo.getDischanargeModeCode(),patientLeaveHospitalVo.getMedicalCategory(),patientLeaveHospitalVo.getMedicalCategoryCode(),
                patientLeaveHospitalVo.getOutDatetime(),patientLeaveHospitalVo.getDiagId(),
                patientLeaveHospitalVo.getCirculationlLogId(),patientLeaveHospitalVo.getStopDateTime(),patientLeaveHospitalVo.getBabyStopDateTime());
    }

    /**
     * 获取出院方式
     */
    @GetMapping("/queryDischargeMode")
    @ApiOperation(value = "出院方式")
    public Result queryDischargeMode(@RequestParam(required = false) String id) {
        return patientCirculationService.queryDischargeMode(id);
    }

    /**
     * 获取医疗类别
     */
    @GetMapping("/queryMedicalCategory")
    @ApiOperation(value = "医疗类别")
    public Result queryMedicalCategory(@RequestParam String inpatNum) {
        return patientCirculationService.queryMedicalCategory(inpatNum);
    }

    /**
     * 获取出院诊断
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    @GetMapping("/queryDischargeDiagnosis")
    @ApiOperation(value = "根据住院号获取出院主诊断")
    public Result queryDischargeDiagnosis(@RequestParam String inpatNum) {
        return patientCirculationService.queryDischargeDiagnosis(inpatNum);
    }

    /**
     * 获取出院诊断等信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    @GetMapping("/queryPatientFinishutHospByInpatNum")
    @ApiOperation(value = "根据住院号获取出院主诊断")
    public Result queryPatientFinishutHospByInpatNum(@RequestParam String inpatNum) {
        return patientCirculationService.queryPatientFinishutHospByInpatNum(inpatNum);
    }

    /**
     *
     * @param inpatNum 住院号
     * @param operation 0-转入手术室
     * @return
     */
    @GetMapping("/patientOperation")
    @ApiOperation(value = "病人手术流转")
    public Result patientOperation(@RequestParam String inpatNum,@RequestParam String operation,@RequestParam(required = false) String remark,@RequestParam String circulationlLogId){
        return patientCirculationService.patientOperation(inpatNum,operation,remark,circulationlLogId);
    }

    @GetMapping("/getOptions")
    @ApiOperation(value = "获取下拉选项")
    public Result getOptions(@RequestParam String dataId){
        return patientCirculationService.getOptions(dataId);
    }

    @GetMapping("/canCancel")
    @ApiOperation(value = "是否可以取消转出")
    public Result canCancel(@RequestParam String inpatNum,@RequestParam String circulationlLogId){
        return patientCirculationService.outCanCancel(inpatNum,circulationlLogId);
    }


    /**
     *
     * @param inpatNum 住院号
     * @return
     */
    @GetMapping("/cancelOperation")
    @ApiOperation(value = "取消病人流转的通用接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "operationFlag", value = "取消操作标记", dataType = "String", paramType = "operationFlag"),
            @ApiImplicitParam(name = "bedNo", value = "床号", dataType = "String", paramType = "bedNo")
    })
    public Result cancelOperation(@RequestParam String inpatNum,
                                  @RequestParam String operationFlag,
                                  @RequestParam(required = false) String bedNo,
                                  @RequestParam String circulationlLogId){
        if(CirculationModelEnum.CANCLE_IN_HOSP.getCode().equals(operationFlag)){
            return patientCirculationService.cancleInHosp(inpatNum,circulationlLogId);
        }else if(CirculationModelEnum.CANCLE_OUT_HOSP.getCode().equals(operationFlag)){
            return patientCirculationService.patientCancelLeaveHospital(inpatNum,bedNo,circulationlLogId);
        }else if(CirculationModelEnum.CANCLE_IN_DEPT.getCode().equals(operationFlag)){//取消入科
            return patientCirculationService.patientTransferInCancel(inpatNum,circulationlLogId);
        }else if(CirculationModelEnum.CANCLE_OUT_DEPT.getCode().equals(operationFlag)){//取消出科
            return patientCirculationService.patientTransferOutCancel(inpatNum,bedNo,circulationlLogId);
        }

        return ResultGenerator.genOkResult();
    }

    /**
     *  获取病区对应的所有医生
     */
    @GetMapping("/queryNurseListByWardDeptId")
    @ApiOperation(value = "获取病区对应的所有医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区Id", dataType = "String", paramType = "wardDeptId")
    })
    public Result queryNurseListByWardDeptId(@RequestParam String wardDeptId){
        return patientCirculationService.queryNurseListByWardDeptId(wardDeptId);
    }


    @GetMapping("/queryPatientOperationByCirculationLogId")
    @ApiOperation(value = "获取手术相关信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "circulationLogId", value = "流转Id", dataType = "String", paramType = "circulationLogId")
    })
    public Result queryPatientOperationByCirculationLogId(@RequestParam String circulationLogId){
        return patientCirculationService.queryPatientOperationByCirculationLogId(circulationLogId);
    }

    @GetMapping("/patientScanWristBandCode")
    @ApiOperation(value = "病人扫腕带")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "二维码腕带", dataType = "String", paramType = "qrCode"),
            @ApiImplicitParam(name = "wardDeptId", value = "病区", dataType = "String", paramType = "wardDeptId"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "startDate"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "endDate"),
    })
    public Result patientScanCode(@RequestParam String qrCode,
                                  @RequestParam String wardDeptId,
                                  @RequestParam(required = false) String startDate,
                                  @RequestParam(required = false) String endDate){
        return patientCirculationService.patientScanWristBandCode(qrCode,wardDeptId,startDate,endDate);
    }

    @GetMapping("/patientScanBedCode")
    @ApiOperation(value = "病人扫床头码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrCode", value = "二维码腕带", dataType = "String", paramType = "qrCode"),
            @ApiImplicitParam(name = "circulationlLogId", value = "流转id", dataType = "String", paramType = "circulationlLogId")
    })
    public Result patientScanBedCode(@RequestParam String qrCode,
                                     @RequestParam String circulationlLogId){
        return patientCirculationService.patientScanBedCode(qrCode,circulationlLogId);
    }


    @GetMapping("/getCirculationType")
    @ApiOperation(value = "获取流转类型")
    public Result getCirculationType(){
        return patientCirculationService.getCirculationType();
    }

    @PostMapping("/queryCirculationLogList")
    @ApiOperation(value = "获取流转日志信息")
    public Result queryCirculationLogList(@RequestBody Map<String, String> params){
        return patientCirculationService.queryCirculationLogList(params);
    }


    @GetMapping("/queryWardDeptByDeptId")
    @ApiOperation(value = "根据科室获取病区")
    public Result queryWardDeptByDeptId(@RequestParam String deptId,@RequestParam(required = false) String id,@RequestParam(required = false) String keyWords){
        return patientCirculationService.queryWardDeptByDeptId(deptId,id,keyWords);
    }


    @GetMapping("/queryPatientInHospAssessmentSheet")
    @ApiOperation(value = "根据住院号获取入院评估单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryPatientInHospAssessmentSheet(@RequestParam String inpatNum){
        return patientCirculationService.queryPatientInHospAssessmentSheet(inpatNum);
    }


    @PostMapping("/updatePatientInfo")
    @ApiOperation(value = "修改病人信息")
    public Result updatePatientInfo(@RequestBody Map<String,String> params){
        return patientCirculationService.updatePatientInfo(params);
    }


    @GetMapping("/queryPatientPayOff")
    @ApiOperation(value = "根据住院号查询费别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum")
    })
    public Result queryPatientPayOff(@RequestParam String inpatNum){
        return patientCirculationService.queryPatientPayOff(inpatNum);
    }

    @PostMapping("/queryEvaluationTime")
    @ApiOperation(value = "获取评估时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "startDate"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "endDate")
    })
    public Result queryEvaluationTime(@RequestBody Map<String, String> params){
        return patientCirculationService.queryEvaluationTime(params);
    }


    @GetMapping("/queryDailyAssessmentSheet")
    @ApiOperation(value = "根据住院号查询费别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelName", value = "模板名称", dataType = "String", paramType = "modelName"),
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "circulationlLogId", value = "流转日志id", dataType = "String", paramType = "circulationlLogId")
    })
    public Result queryDailyAssessmentSheet(@RequestParam final String modelName, @RequestParam final String inpatNum,@RequestParam String circulationlLogId){
        return patientCirculationService.queryDailyAssessmentSheet(modelName,inpatNum,circulationlLogId);
    }

    @GetMapping("/outDiagnosis")
    @ApiOperation(value = "获取出院诊断接口")
    public Result outDiagnosis(@RequestParam(defaultValue = "",required = false)  String keyWords, @RequestParam(defaultValue = "1")  Integer page,
                               @RequestParam(defaultValue = "20")  Integer pageSize,@RequestParam(required = false) String id){
        return patientCirculationService.outDiagnosis(keyWords,page,pageSize,id);
    }


    @GetMapping("/queryAssessmentScale")
    @ApiOperation(value = "获取评估量表")
    public Result queryAssessmentScale(@RequestParam("modelName") String modelName,
                                       @RequestParam("inpatNum") String inpatNum,
                                       @RequestParam(required = false) String keyWords){
        return patientCirculationService.queryAssessmentScale(modelName,inpatNum,keyWords);
    }


    @GetMapping("/createAssessmentScale")
    @ApiOperation(value = "获取评估量表")
    public Result createAssessmentScale(@RequestParam final String tplId,
                                        @RequestParam final String inpatNum,
                                        @RequestParam final String wardDeptId,
                                        @RequestParam(required = false) String uuid){
        //根据住院号获取科室id
        Map<String,String> patient = patientCirculationService.getPatientInfo(inpatNum);
        if(patient!=null) {
            return assessmentScaleService.scoreCreate(tplId,inpatNum,patient.get("deptId"),uuid);
        }
        return ResultGenerator.genOkResult();
    }

    @PostMapping("/saveAssessmentScale")
    @ApiOperation(value = "保存评估量表")
    public Result saveAssessmentScale(@RequestBody final String json){
        return assessmentScaleService.saveFileIndex(json);
    }


    @GetMapping("/getVteDetail")
    @ApiOperation(value = "获取病人评估明细表格")
    public Result getVteDetail (@RequestParam final String inpatNum,@RequestParam final String catalogId,
                                @RequestParam final String strTm,@RequestParam final String endTm) {
        return assessmentScaleService.getVteDetail(inpatNum+"-1",catalogId,strTm,endTm);
    }


    @GetMapping("/{emrFileId}")
    @ApiOperation(value = "获取病历")
    public Result getFileIndex(@PathVariable final String emrFileId) {
       return assessmentScaleService.getFileIndex(emrFileId);
    }

    @DeleteMapping("/{emrFileId}")
    @ApiOperation(value = "删除病历")
    public Result deleteFileIndex(@PathVariable final String emrFileId) {
        return assessmentScaleService.deleteFileIndex(emrFileId);
    }


    @GetMapping("/queryNewHospitalList")
    @ApiOperation(value="PC端新入院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "allFlag", value = "0 不勾选 1勾选", dataType = "String", paramType = "allFlag"),
            @ApiImplicitParam(name = "operationFlag", value = "wait_in_hosp,wait_out_hosp,wait_out_dept,wait_in_dept", dataType = "String", paramType = "allFlag"),
    })
    public Result queryWaitNewHospitalList(@RequestParam(required = false) String wardDeptId,@RequestParam(required = false) String allFlag,
                                           @RequestParam(required = false) String keyWords,@RequestParam(defaultValue = "1")  Integer page,
                                           @RequestParam(defaultValue = "20")  Integer pageSize,
                                           @RequestParam(required = false) String operationFlag){
        return patientCirculationService.queryWaitNewHospitalList(wardDeptId,allFlag,keyWords,page,pageSize,operationFlag);
    }


    @GetMapping("/queryPatientInfo")
    @ApiOperation(value="PC端新入院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "allFlag", value = "0 不勾选 1勾选", dataType = "String", paramType = "allFlag")
    })
    public Result queryPatientInfo(@RequestParam(required = false) String circulationLogId,
                                   @RequestParam(required = false) String inpatNum,
                                   @RequestParam(required = false) String operationFlag){
        return patientCirculationService.queryPatientInfo(circulationLogId,inpatNum,operationFlag);
    }


    /**
     * 获取下拉框信息
     * @param id
     * @return
     */
    @GetMapping("/queryDropDownList")
    @ApiOperation(value="获取下拉框信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataVersionId", value = " 177-国籍 166-身份证类型 121-民族 69-职业 414-婚姻 352-省,市，县", dataType = "String", paramType = "dataVersionId"),
            @ApiImplicitParam(name = "serialNumber", value = "选择省市县", dataType = "String", paramType = "serialNumbe"),
            @ApiImplicitParam(name = "dataValueId", value = "选中的值", dataType = "String", paramType = "dataValueId")
    })
    public Result queryDropDownList(@RequestParam(required = false) String dataVersionId,
                                @RequestParam(required = false) String serialNumber,
                                @RequestParam(required = false) String id,
                                @RequestParam(required = false) String keyWords,
                                @RequestParam(defaultValue = "1")  Integer page,
                                @RequestParam(defaultValue = "20")  Integer pageSize){
        return patientCirculationService.queryDropDown(dataVersionId,serialNumber,id,keyWords,page,pageSize);
    }

    /**
     * 获取宗教信息
     * @param id
     * @return
     */
    @GetMapping("/queryReligionList")
    @ApiOperation(value="获取下拉框信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basicdatavalueId", value = "诊断ID", dataType = "String", paramType = "diagId")
    })
    public Result queryReligionList(@RequestParam(required = false) String id,
                                     @RequestParam(required = false) String keyWords,
                                     @RequestParam(defaultValue = "1")  Integer page,
                                     @RequestParam(defaultValue = "20")  Integer pageSize){
        return patientCirculationService.queryReligionList(id,keyWords,page,pageSize);
    }

    /**
     * 获取诊断信息
     * @param id
     * @return
     */
    @GetMapping("/queryDiagnosisList")
    @ApiOperation(value="获取下拉框信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diagId", value = "诊断ID", dataType = "String", paramType = "diagId")
    })
    public Result queryDiagnosisList(@RequestParam(required = false) String id,
                                     @RequestParam(required = false) String keyWords,
                                     @RequestParam(defaultValue = "1")  Integer page,
                                     @RequestParam(defaultValue = "20")  Integer pageSize){
        return patientCirculationService.queryDiagnosisList(id,keyWords,page,pageSize);
    }

    /**
     * 获取手术信息
     */
    @GetMapping("/queryOperationList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diagId", value = "诊断ID", dataType = "String", paramType = "diagId")
    })
    public Result queryOperationList(@RequestParam(required = false) String id,
                                     @RequestParam(required = false) String keyWords,
                                     @RequestParam(defaultValue = "1")  Integer page,
                                     @RequestParam(defaultValue = "20")  Integer pageSize){
        return patientCirculationService.queryOperationList(id,keyWords,page,pageSize);
    }

    /**
     * 保存新入院
     * @param patientInHospitalEntity
     * @return
     */
    @PostMapping("/saveNewHospitalList")
    @ApiOperation(value="获取下拉框信息ce")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "diagId", value = "诊断ID", dataType = "String", paramType = "diagId")
    })
    public Result saveNewHospitalList(@RequestBody PatientInHospitalDTO patientInHospitalEntity){
        return patientCirculationService.saveNewHospitalList(patientInHospitalEntity);
    }

    /**
     * 转科转入保存 ce
     */
    @PostMapping("/saveTranferInDept")
    @ApiOperation(value="转科转入保存")
    public Result saveTranferInDept(@RequestBody PatientInHospitalDTO patientInHospitalEntity){
        return patientCirculationService.saveTranferInDept(patientInHospitalEntity);
    }

    /**
     * 取消入院查询
     * @param wardId
     * @param bedNo
     * @return
     */
    @GetMapping("/queryFinishNewHospitalList")
    @ApiOperation(value="取消入院查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "取消操作标记", value = "cancle_in_hosp、cancle_out_hosp、cancle_in_dept、cancle_out_dept", dataType = "String", paramType = "operationFlag")
    })
    public Result queryFinishNewHospitalList(@RequestParam(required = false) String wardId,
                                             @RequestParam(required = false) String bedNo,
                                             @RequestParam(required = false) String operationFlag,
                                             @RequestParam(required=false) String inpatNum,
                                             @RequestParam(required=false) String healthNum){
        return patientCirculationService.queryFinishNewHospitalList(wardId,bedNo,operationFlag,inpatNum,healthNum);
    }

    @GetMapping("/queryBedByWardDeptId2")
    @ApiOperation(value = "病区可用床号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区id", dataType = "String", paramType = "deptId")
    })
    public Result queryBedByWardDeptId2(@RequestParam String wardDeptId,
                                        @RequestParam(required=false) String id,
                                        @RequestParam(defaultValue = "1")  Integer page,
                                        @RequestParam(defaultValue = "20")  Integer pageSize,
                                        @RequestParam(required = false) String keyWords,
                                        @RequestParam(required = false) String operationFlag) {
        return patientCirculationService.queryBedByWardDeptId2(wardDeptId,id,page,pageSize,keyWords,operationFlag);
    }

    @GetMapping("/queryDepDoctor2")
    @ApiOperation(value = "科室对应所有医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depId", value = "科室id", dataType = "String", paramType = "depId")
    })
    public Result queryDepDoctor2(@RequestParam(required=false) String depId,@RequestParam(required=false) String id,@RequestParam(defaultValue = "1")  Integer page,
                                  @RequestParam(defaultValue = "20")  Integer pageSize,@RequestParam(required = false) String keyWords) {
        return patientCirculationService.queryDepDoctorAndNursList2(depId,id,page,pageSize,keyWords);
    }


    /**
     *  获取病区对应的所有医生
     */
    @GetMapping("/queryNurseListByWardDeptId2")
    @ApiOperation(value = "获取病区对应的所有医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区Id", dataType = "String", paramType = "wardDeptId")
    })
    public Result queryNurseListByWardDeptId2(@RequestParam String wardDeptId,
                                              @RequestParam(required=false) String id,
                                              @RequestParam(defaultValue = "1")  Integer page,
                                              @RequestParam(defaultValue = "20")  Integer pageSize,
                                              @RequestParam(required = false) String keyWords){
        return patientCirculationService.queryNurseListByWardDeptId2(wardDeptId,id,page,pageSize,keyWords);
    }

    @GetMapping("/depts2")
    @ApiOperation(value = "获取授权科室或病区")
    public Result getDepts(@RequestParam(required=false) String id,
                           @RequestParam(defaultValue = "1")  Integer page,
                           @RequestParam(defaultValue = "20")  Integer pageSize,
                           @RequestParam(required = false) String keyWords) {
        return patientService.getDepts2(id,page,pageSize,keyWords);
    }


    @GetMapping("/queryDeptByWardDeptId2")
    @ApiOperation(value = "根据病区找对应科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "dept/wardDept", dataType = "String", paramType = "type"),
            @ApiImplicitParam(name = "contentId", value = "病区id", dataType = "String", paramType = "deptId")
    })
    public Result queryDeptByWardDeptId2(@RequestParam(required = false) final String type,
                                         @RequestParam final String contentId,
                                         @RequestParam(required=false) String id,
                                         @RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "20")  Integer pageSize,
                                         @RequestParam(required = false) String keyWords) {
        return patientCirculationService.queryDeptByWardDeptId2(type, contentId,id,page,pageSize,keyWords);
    }


    @GetMapping("/queryBedNoByWardDeptId")
    @ApiOperation(value = "根据病区获取已有床号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区id", dataType = "String", paramType = "wardId")
    })
    public Result queryBedNoByWardDeptId(@RequestParam(required = false) String wardId,@RequestParam(required=false) String id,
                                         @RequestParam(required = false) String keyWords,
                                         @RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "20")  Integer pageSize) {
        return patientCirculationService.queryBedNoByWardDeptId(wardId,id,page,pageSize,keyWords);
    }


    @GetMapping("/queryDeptList")
    @ApiOperation(value = "获取所有科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区id", dataType = "String", paramType = "wardId")
    })
    public Result queryDeptList(@RequestParam(required=false) String id,
                                         @RequestParam(required = false) String keyWords,
                                         @RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "20")  Integer pageSize) {
        return patientCirculationService.queryDeptList(id,page,pageSize,keyWords);
    }

    @GetMapping("/queryInDeptByInpatNum")
    @ApiOperation(value = "根据住院号和会员Id查询待转入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区id", dataType = "String", paramType = "wardId")
    })
    public Result queryInDeptByInpatNum(@RequestParam(required = false) String wardId,
                                        @RequestParam(required=false) String inpatNum,
                                        @RequestParam(required=false) String healthNum,
                                        @RequestParam(required=false) String operationFlag) {
        return patientCirculationService.queryInDeptByInpatNum(wardId,inpatNum,healthNum,operationFlag);
    }

    @GetMapping("/patientCheckLeaveHospital")
    @ApiOperation(value = "出院操作前数据检查接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNum", value = "住院号", dataType = "String", paramType = "inpatNum"),
            @ApiImplicitParam(name = "operationFlag", value = "操作标记", dataType = "String", paramType = "operationFlag"),
    })
    public Result patientCheckLeaveHospital(@RequestParam(required = false) String inpatNum,@RequestParam(required = false) String operationFlag){
        return patientCirculationService.patientCheckLeaveHospital(inpatNum,operationFlag);
    }



















}